package me.pinion.model.message;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record
import me.pinion.model.ModelType
import me.pinion.model.User;
import me.pinion.model.XModel;
import me.pinion.model.annotation.TableBind;

@TableBind(table = "message")
public class Message extends XModel<Message> {
    public static Message dao = new Message();

    public boolean read(){ return false;}

    public MessageType getMsgType(){
        Record record = Db.findById("msg_type", this.getInt("msg_type"));
        return MessageType.valueOf(record.getStr("name"));
    }

    public User getSender(){
        return User.dao.findById(this.getLong("sender"));
    }
    public User getReciver(){
        return User.dao.findById(this.getLong("reciver"));
    }

    public XModel<? extends XModel> getSource(){
        ModelType modelType = ModelType.dao.findById(getInt("source_type_id"))
        Class<? extends XModel> clazz = modelType.getModelClass()
        return clazz.cast(modelType.getModel(this.getInt("source_id")))
    }

    public ModelType getSourceType(){
        return ModelType.dao.findById(getInt("source_type_id"))
    }
}